import { getCartInfo,setCartInfo,creatCartOne} from "../../api/cart.js";
import { getCollectInfo,deleteCollectOne,createCollectOne} from "../../api/collect.js";
import {   request} from "../../request/index";

Page({

    data: {
        goodsObj: {},
        isCollect: false,
        goods_id:1,
        userid:1,
        id:1,
        thisCartId:null
    },
    GoodsInfo: {},

    /**
     * 生命周期函数--监听页面加载
     */
    onShow: function() {
         let userid = wx.getStorageSync('userid')
        let pages = getCurrentPages();
        let currentPage = pages[pages.length - 1];
        let options = currentPage.options;
        const {
            goods_id
        } = options;
        console.log(goods_id);
        this.setData({
            userid,
            goods_id
        })
        this.getGoodsDetail(goods_id);
        this.getCollectInfo()
        this.getCartId()
      
    },

    async getCartId(){
        const userid = wx.getStorageSync('userid')
        const goods_id = this.data.goods_id
        const cart = await getCartInfo(userid)
        const goods= cart.find(v=>v.goods_id==goods_id)
        if(goods){
        this.setData({
            thisCartId:goods.id
        })
    }else{
        this.setData({
            thisCartId:null
        })
    }
    },

    async getCollectInfo(){
        const collectall = await getCollectInfo(this.data.userid)
      console.log(collectall);
       const collect = collectall.find(v=>v.goods_id===this.data.goods_id)
      if(collect){
        this.setData({
            isCollect:true,
           id:collect.id
        })
      }else{
        this.setData({
            isCollect:false,
            id:null
        })
        
      }
    },

    async getGoodsDetail(goods_id) {
        const goodsObj = await request({
            url: "/goodsall/"+goods_id+"/",
        });
        this.GoodsInfo = goodsObj;
        this.setData({
            goodsObj,
        })
    },

    handlePreviewImage(e) {
        const urls = this.GoodsInfo.pics.map(v => v.pics_mid);
        const current = e.currentTarget.dataset.url;
        wx.previewImage({
            current: current,
            urls: urls

        });
    },

    //点击加入购物车
    async handleCartAdd() {
        const thisCartId = this.data.thisCartId
        const userid = wx.getStorageSync('userid')
        const goods_id =this.data.goods_id
        console.log(goods_id);
        if(!thisCartId){
        const result = await  creatCartOne(userid,goods_id);
     wx.showToast({
        title: '加入成功',
        icon: 'success',
        mask: true
    })
    }else{
            const result2 = await setCartInfo (userid,thisCartId,"+")    
          wx.showToast({
            title: '商品数量加1',
            icon: 'success',
            mask: true
        })
    }
        let cart = wx.getStorageSync("cart") || [];
        let index = cart.findIndex(v => v.goods_id === this.GoodsInfo.goods_id);
        if (index === -1) {
            this.GoodsInfo.num = 1;
            this.GoodsInfo.checked = true;
            cart.push(this.GoodsInfo);
        } else {
            cart[index].num++;
        }
        wx.setStorageSync('cart', cart);
       
    },

    async handleBuyRightNow(){
        const thisCartId = this.data.thisCartId
        const userid = wx.getStorageSync('userid')
        const goods_id =this.data.goods_id
        console.log(goods_id);
        if(!thisCartId){
        const result = await creatCartOne (userid,goods_id) 
    }else{
            const result2 = await  setCartInfo (userid,thisCartId,'+') 
         }
    wx.switchTab({
        url: '/pages/cart/index',
    });
    },

   async handleCollect() {
       let collect_id = this.data.id
        let isCollect = this.data.isCollect;
        let userid =this.data.userid
        let goods_id = this.data.goods_id
        console.log(userid);
        if(!isCollect){
            const result = await createCollectOne(userid,goods_id)
          if(result===201){
            wx.showToast({
                title: '收藏成功',
                icon: 'success',
                mask: true,

            });
            this.getCollectInfo()
          }
        
        }else{
            const result2 = await deleteCollectOne(userid,collect_id)
          this.getCollectInfo()
                 wx.showToast({
                title: '取消成功',
                icon: 'success',
                mask: true,
            });
        }
        let collect = wx.getStorageSync('collect') || [];
        let index = collect.findIndex(v => v.goods_id === this.GoodsInfo.goods_id);
        wx.setStorageSync("collect", collect);
    }
})